from flask import Flask, redirect, url_for, request, render_template, flash
import json, re

import _log

####################################################################################################

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/reg_get', methods=['POST', 'GET'])
def reg_get():
    if request.method == 'POST':
        try:
            val_reg = request.form['reg']
            val_content = request.form['content']
        except Exception as e:
            _log.error('%s' % e)
            return 'get reg/content fail!'

        pattern = re.compile(val_reg)

        # 按行分割
        list_s = val_content.split('\n')

        ret_count = 0
        ret_str = ''
        for s in list_s:
            all = pattern.findall(s)

            for one in all:
                ret_str += one + '\n'
                ret_count += 1
        
        ret_json = {
            'ret_count': ret_count, 
            'ret_str': ret_str
        }

        return json.dumps(ret_json)

    return 'invalid operation!'

if __name__ == '__main__':
    # 模块
    _log.init(app)

    # 网页
    app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

    app.run(
        host='127.0.0.1',
        port= 7000,
        debug=True,
        threaded=True
    )
